package com.mjfeed.service.impl;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.mjfeed.Constants;
import com.mjfeed.FileInfo.FileType;
import com.mjfeed.service.FileService;

@Service("fileService")
public class FileServiceImpl implements FileService {

	public boolean isValid(FileType fileType, MultipartFile multipartFile) {
		if (multipartFile == null) {
			return false;
		}
		if (multipartFile.getSize() > Constants.UPLOAD_MAX_SIZE) {
			return false;
		}
		String fileExtensions = "";
		if (fileType == FileType.image) {
			fileExtensions = Constants.UPLOAD_IMAGE_EXTENSION;
		}
		String[] uploadExtensions = fileExtensions.split(",");
		if (ArrayUtils.isNotEmpty(uploadExtensions)) {
			return FilenameUtils.isExtension(multipartFile.getOriginalFilename().toLowerCase(), uploadExtensions);
		}
		return false;
	}

}
